using System;
using Assets.Common.Unity.GameSystem;
using UnityEngine;
using UnityEngine.Serialization;
[Flags]
public enum TeamLayer
{
    L0 = 0x1,
    L1 = 0x2,
    L2 = 0x4,
    L3 = 0x8,
    L4 = 0x10,
    L5 = 0x20,
    L6 = 0x40,
    L7 = 0x80,
    L8 = 0x100,
    L9 = 0x200,
    L10 = 0x400,
    L11 = 0x800,
    L12 = 0x1000,
    L13 = 0x2000,
    L14 = 0x4000,
    L15 = 0x8000,

    
}
public static class EnumExtend
{
    public static bool IsMasked(this TeamLayer teamLayer,int val)
    {
        return  ((int)teamLayer & val) > 0;
    }
}
public class TeamComponentData : SimplyComponentData<TeamComponentData>, ISimplyComponentData,
    IEquatable<TeamComponentData>,ITeamData
{
    //protected override ISimplySystem<TeamComponentData> SimplySystem => TeamSystem.Instance;


    protected override TeamComponentData self => this;

    [Tooltip("阵营，影响攻击目标ai选取")] [SerializeField]
    private TeamLayer _team = TeamLayer.L0;

    [FormerlySerializedAs("_enimyLayerMask")] [SerializeField] private TeamLayer _enimyTeamLayerMask;
    public int Team => (int)_team;

    public void ChangeTeam(int team)
    {
        _team = (TeamLayer)team;
    }

    public bool Equals(TeamComponentData other)
    {
        if (other is null) return false;
        return other._team == _team;
    }

    public bool IsEnemy(TeamComponentData b)
    {
        return ((int)_enimyTeamLayerMask & b.Team) > 0;
    }

    /// <summary>
    /// 无组件认为不是敌人
    /// </summary>
    /// <param name="b"></param>
    /// <returns></returns>
    public bool IsEnemy(Transform b)
    {
        if (b.TryGetComponent<TeamComponentData>(out var team))
        {
            return ((int)_enimyTeamLayerMask & team.Team) > 0;
        }

        return false;
    }


    /// <summary>
    /// 玩家操控的单位
    /// </summary>
    /// <returns></returns>
    public bool IsPlayer()
    {
        return _team == TeamLayer.L0;
    }

    /// <summary>
    /// 无team 组件被认为不同
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    public static bool IsSameTeam(Transform a, Transform b)
    {
        var teamA = a.GetComponent<TeamComponentData>();
        var teamB = b.GetComponent<TeamComponentData>();
        if (teamA is null || teamB is null)
        {
            return false;
        }

        return (teamA.Team == teamB.Team);
    }
}